package com.xbdun.service.bmp.facade;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xbdun.common.api.Client;
import com.xbdun.common.api.XbdUnStringResult;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.common.util.CommonUtil;
import com.xbdun.facade.bmp.request.system.SysOrgFiledsRequest;
import com.xbdun.facade.bmp.result.system.SysOrgFiledsResult;
import com.xbdun.facade.bmp.service.system.SysOrgFiledsFacade;
import com.xbdun.service.bmp.service.system.SysOrgFiledsService;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: SysOrgFiledsFacadeImpl
 * @Description: 机构附属信息 
 * @category 机构附属信息
 * @author wwq@xbdun.com
 * @date 2017年6月16日 下午3:40:05
 */
@Component
@Slf4j
public class SysOrgFiledsFacadeImpl implements SysOrgFiledsFacade {

	@Autowired
	private SysOrgFiledsService sysOrgFiledsService;

	public XbdUnStringResult addSysOrgFileds(SysOrgFiledsRequest request,Client client) {
		XbdUnStringResult result = new XbdUnStringResult();
		try {
			log.info("start - 新增机构附属信息初始参数,{}", request);
			CommonUtil.validateClient(client);
			sysOrgFiledsChecked(request);
			sysOrgFiledsService.addSysOrgFileds(request,client);
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("error - 新增机构附属信息失败,入参:{},异常:{}", request, ex);
		} finally {
			log.info("end - 新增机构附属信息结果:{}", result);
		}
		return result;
	}

	public SysOrgFiledsResult getSysOrgFiledsByOrgCode(Client client) {
		SysOrgFiledsResult result = new SysOrgFiledsResult();
		try {
			CommonUtil.validateClient(client);
			result.setData(sysOrgFiledsService.getSysOrgFiledsByOrgCode(client.getOrgCode()));
		} catch (Exception e) {
			result.setBizException(e);
			log.error("error - 获取机构附属信息失败,入参:{},异常:{}", client, e);
		}
		return result;
	}

	public XbdUnStringResult modifySysOrgFileds(SysOrgFiledsRequest request, Client client) {
		XbdUnStringResult result = new XbdUnStringResult();
		try {
			log.info("start - 修改机构附属信息入参:" + request);
			if(client.getOrgCode()!=0){
				CommonUtil.validateClient(client);
			}
			sysOrgFiledsChecked(request);
			sysOrgFiledsService.modifySysOrgFileds(request,client);
		} catch (Exception e) {
			result.setBizException(e);
			log.error("error - 修改机构附属信息失败,入参:{},异常:{}", request, e);
		} finally {
			log.info("end - 修改机构附属信息入参{}", result);
		}
		return result;
	}
	// 检测机构附属信息
	private void sysOrgFiledsChecked(SysOrgFiledsRequest request)throws BizException {
		if (null == request){
			throw BizException.PARAM_CHKE_FAIL.newInstance("请求对象不能为空");
		}
		StringBuilder sb = new StringBuilder();
		if (sb.length() > 0) {
			throw BizException.PARAM_CHKE_FAIL.newInstance("%s", sb.toString());
		}
	}

}
